package com.yanqu.road.server.cmd.activity.summergames;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesMedalsSort;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_SUMMER_GAMES_EXCHANGE_LIST, desc = "交换大厅")
@CmdApi(reqMsg = SummerGamesProto.SummerGamesExchangeListReqMsg.class, respMsg = SummerGamesProto.SummerGamesExchangeListRespMsg.class)
public class SummerGamesExchangeListCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerGamesUserData userData = player.getModule(SummerGamesModule.class).getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        SummerGamesProto.SummerGamesExchangeListReqMsg reqMsg = SummerGamesProto.SummerGamesExchangeListReqMsg.parseFrom(packet.getBytes());
        if(reqMsg.getType() < 0 || reqMsg.getType() > 2){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
//        if(reqMsg.getType() == 2 && reqMsg.getAliasId() <= 0){
//            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
//            return;
//        }
        int pageNum = 0;
        if(reqMsg.hasPageNum()){
            pageNum = reqMsg.getPageNum();
        }
        if(pageNum <= 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }


        SummerGamesProto.CrossSummerGamesExchangeListReqMsg.Builder crossReq = SummerGamesProto.CrossSummerGamesExchangeListReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setType(reqMsg.getType());
        crossReq.setAliasId(reqMsg.getAliasId());
        crossReq.setUnionUid("");
        crossReq.setUserId(0);
        crossReq.setPageNum(pageNum);
        crossReq.addAllMedals(reqMsg.getMedalsList());

        if(reqMsg.getMedalsList().size() > 0){
            for (Integer integer : reqMsg.getMedalsList()) {
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(integer);
                if(goodsInfo == null){
                    ret = GameErrorCode.E_GOODS_NO_EXIST;
                    return;
                }
            }
        }

        //带上拥有的道具
        List<GoodsInfo> goodsListByType = GoodsMgr.getGoodsListByType(eGoodsType.SummerGamesMedals.getValue());
        List<UserBagItem> list = new ArrayList<>();
        for (GoodsInfo info : goodsListByType) {
//            if(info.getQuality() != goodsInfo.getQuality()){
//                continue;
//            }
            UserBagItem userBagItem = player.getModule(BagModule.class).getUserBagItem(info.getGoodsId());
            if(userBagItem == null || userBagItem.getNum() <= 0){
                continue;
            }
            list.add(userBagItem);
        }
        list.sort(new SummerGamesMedalsSort());
        for (UserBagItem bagItem : list) {
            crossReq.addOwnMedals(bagItem.getGoodsId());
        }


        if(reqMsg.getType() == 1){
            String unionUid = player.getUserInfo().getUnionUid();
            if(StringUtils.isNullOrEmpty(unionUid)) {
                ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
                return;
            }
            crossReq.setUnionUid(unionUid);
        }else if(reqMsg.getType() == 2){
//            Map<Long, Long> map = UserMgr.getUserServerByUserAliasId(reqMsg.getAliasId(), config.getActivityId());
//            if(map.isEmpty()){
//                ret = GameErrorCode.E_USER_NO_FOUND;
//                return;
//            }
//            for (Map.Entry<Long, Long> entry : map.entrySet()) {
//                if(entry.getKey() == player.getUserId()){
//                    continue;
//                }
//                crossReq.setUserId(entry.getKey());
//                break;
//            }
//            if(crossReq.getUserId() <= 0){
//                ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
//                return;
//            }
        }

        player.sendPacket(Protocol.C_SUMMER_GAMES_EXCHANGE_LIST, crossReq);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_EXCHANGE_LIST, SummerGamesProto.SummerGamesExchangeListRespMsg.newBuilder().setRet(ret));
        }
    }
}
